package com.yuke.cloud.service.uac.security;

import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.security.core.SecurityUser;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.service.AccEmployeeGroupService;
import com.yuke.cloud.service.uac.service.AccUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * The class Uac user details service.
 *
 * @author
 */
@Component
@Slf4j
public class UacUserDetailsServiceImpl implements UserDetailsService {

	@Resource
	private AccUserService accUserService;
	@Resource
	private AccEmployeeGroupService accEmployeeGroupService;
	/**
	 * Load user by username user details.
	 *
	 * @param username the username
	 *
	 * @return the user details
	 */
	@Override
	public UserDetails loadUserByUsername(String username) {
		Collection<GrantedAuthority> grantedAuthorities;
		log.info("UacUserDetailsServiceImpl==>loadUserByUsername ==username={}", username);
		// add by wg 20181201增加手机号登录时根据手机号获取用户信息
		String userNameTemp = username;
		try {
			JSONObject myJson = JSONObject.parseObject(username);
			userNameTemp = myJson.getString("mobile");
			userNameTemp = findUserNameByMobileNo(userNameTemp);
		} catch (Exception e) {
			userNameTemp = username;
		}

		log.info("===============username={}, userNameTemp={}", username, userNameTemp);

		AccUser user = accUserService.findByLoginName(userNameTemp);
		if (user == null && PublicUtil.isNotEmpty(userNameTemp)) {
			// add by wg 20190802 如果无法根据账号找到用户名，尝试根据手机号查找
			Example example = new Example(AccUser.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("mobile", userNameTemp);
			List<AccUser> users = accUserService.selectByExample(example);

			if (users != null && users.size() > 0) {
				user = users.get(0);
			}
		}

		if (user == null) {
			throw new BadCredentialsException("用户不存在或者密码错误");
		}
		//判断员工还是手机用户
		if (user.getIsEmployee()){
			user = accEmployeeGroupService.selectUserInfoByUserId(user.getUserId());
		}else{
			user = accUserService.findUserInfoByUserId(user.getUserId());
		}

		grantedAuthorities = accUserService.loadUserAuthorities(user.getUserId());
		return new SecurityUser(user.getUserId(), user.getLoginName(), user.getUserPassword(),
				user.getUserName(), user.getGroupId(), user.getGroupName(), user.getIsEnable(), grantedAuthorities);
	}

	// add by wg 20181201增加手机号登录时根据手机号获取用户信息
	private String findUserNameByMobileNo(String mobileNo) {
		AccUser user = accUserService.findByMobileNo(mobileNo);
		if (user != null) {
			return user.getLoginName();
		}

		return null;
	}
}
